import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import LayOut from "@/layout/index.vue"
import { getToken } from "@/utils/auth"
import store from '@/store'

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        component: LayOut,
        redirect: '/',
        children: [
            {
                path: "",
                name: 'home',
                component: () => import("../views/Home/index.vue"),
                meta: {
                    keepAlive: true,
                    index: 1
                }
            },
            {
                path: "/user",
                component: () => import("../views/user/index.vue"),
                meta: {
                    index: 1
                }
            },
            {
                path: "/car",
                component: () => import("../views/car/index.vue"),
                meta: {
                    index: 1
                }
            },
            {
                path: "/category",
                component: () => import("../views/category/index.vue"),
                meta: {
                    index: 1
                }
            },
        ]
    },
    {
        path: "/login",
        component: () => import("../views/login/index.vue"),
    },
    {
        path: "/about",
        component: () => import("../views/about/index.vue"),
        meta: {
            index:2
        }
    },
    {
        path: "/address",
        component: () => import("../views/address/index.vue"),
        meta: {
            index:2
        }
    },
    {
        path: "/setting",
        component: () => import("../views/setting/index.vue"),
        meta: {
            index:2
        }
    },
    {
        path: "/order",
        component: () => import("../views/order/order-list.vue"),
        meta: {
            index:2
        }
    },
    {
        path: "/create-order",
        component: () => import("../views/order/create-order.vue"),
    },
    {
        path: "/order-detail",
        component: () => import("../views/order/order-detail.vue"),
    },
    {
        path: "/address-add",
        component: () => import("../views/address/add.vue"),
    },
    {
        path: "/address-edit",
        component: () => import("../views/address/add.vue"),
    },
    {
        path: "/product/:id",
        component: () => import("../views/product/index.vue"),
    },
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
});

const blackList: string[] = ['/category']

router.beforeEach(async (to: any, from, next) => {

    // 根据token判断用户是否登录
    const hasToken = getToken()
    if (hasToken) {
        if (to.path === '/login') {
            next({ path: '/' })
        } else {
            store.commit('user/setToken', hasToken)
            next()
        }
    } else {
        if (blackList.indexOf(to.path) === -1) {
            next()
        } else {
            next(`/login?redirect=${to.fullPath}`)
        }
    }
})


export default router;
